// pages/secretary/secretaryInfo/secretaryInfo.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 秘书团
        secretaryArr: [],
        secretaryInfo: false,
        secretaryPersonNum: 0,
        // 设置身份
        id: '',
        uid: '',
        level: '',
        //操作弹框
        show: false,
        actions: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getHomePageCityInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    // 查看他人主页
    jumpOthersPage(e) {
        wx.navigateTo({
            url: `../../../functionPage/pages/othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
        });
    },
    // 长按设置身份
    setIdentityBtn(e) {
        let action = [];
        if (
            app.globalData.authority == '00' ||
            app.globalData.authority == '01'
        ) {
            if (e.currentTarget.dataset.level == '00') {
                action = [
                    {
                        name: '取消职务',
                        color: '#333333',
                    },
                    {
                        name: '设置为副团长',
                        color: '#333333',
                    },
                ];
            } else if (e.currentTarget.dataset.level == '01') {
                action = [
                    {
                        name: '取消职务',
                        color: '#333333',
                    },
                    {
                        name: '设置为团长',
                        color: '#333333',
                    },
                ];
            } else {
                action = [
                    {
                        name: '设置为团长',
                        color: '#333333',
                    },
                    {
                        name: '设置为副团长',
                        color: '#333333',
                    },
                ];
            }
        } else if (app.globalData.authority == '04') {
            if (e.currentTarget.dataset.level == '01') {
                action = [
                    {
                        name: '取消职务',
                        color: '#333333',
                    },
                ];
            } else {
                action = [
                    {
                        name: '设置为副团长',
                        color: '#333333',
                    },
                ];
            }
        } else {
            return;
        }

        this.setData({
            show: true,
            actions: action,
            uid: e.currentTarget.dataset.uid,
            id: e.currentTarget.dataset.id,
            level: e.currentTarget.dataset.level || '',
        });
    },
    //取消
    onCancel(e) {
        this.setData({
            show: false,
        });
    },
    onDelect(e) {
        let _that = this;
        let flag = ''; //flag	 00设置团长、副团长 01取消身份
        let level = ''; //00团长 01副团长
        console.log(e.detail.name);
        if (e.detail.name == '取消职务') {
            if (app.powerIdentity('00,01,04')) return; //权限判断
            flag = '01';
            _that.setSecretariat(flag, level);
        } else if (e.detail.name == '设置为团长') {
            if (app.powerIdentity('00,01')) return; //权限判断
            flag = '00';
            level = '00';
            if (_that.data.secretaryArr[0].level == '00') {
                //存在团长
                wx.showModal({
                    title: '提示：',
                    content: '当前已存在团长，确认替换吗？',
                    success: function (res) {
                        if (res.confirm) {
                            _that.setSecretariat(flag, level);
                        } else {
                            console.log('点击取消回调');
                        }
                    },
                });
            } else {
                _that.setSecretariat(flag, level);
            }
        } else if (e.detail.name == '设置为副团长') {
            if (app.powerIdentity('00,01,04')) return; //权限判断
            flag = '00';
            level = '01';
            if (
                _that.data.secretaryArr[1].level == '01' ||
                _that.data.secretaryArr[0].level == '01'
            ) {
                //存在副团长
                wx.showModal({
                    title: '提示：',
                    content: '当前已存在副团长，确认替换吗？',
                    success: function (res) {
                        if (res.confirm) {
                            _that.setSecretariat(flag, level);
                        } else {
                            console.log('点击取消回调');
                        }
                    },
                });
            } else {
                _that.setSecretariat(flag, level);
            }
        } else {
            this.setData({
                show: false,
            });
        }
    },
    // 设置成员身份
    setSecretariat(flag, level) {
        let _that = this;
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/setCitySecretariat?flag=${flag}&authId=${app.globalData.authId}`,
            method: 'POST',
            header: {
                'content-type': 'application/json', // 默认值
            },
            data: {
                id: _that.data.id,
                uid: _that.data.uid,
                level: level,
                city: app.globalData.addressBack,
                country: app.globalData.countryBack,
            },
            success: function (res) {
                console.log(res.data);
                _that.getHomePageCityInfo();
                _that.setData({
                    show: false,
                });
            },
        });
    },
    // 获取秘书团成员
    getHomePageCityInfo() {
        let _that = this;
        wx.request({
            url: app.globalData.rootPath + `code/getHomePageCityInfo`,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded', // 默认值
            },
            data: {
                country: app.globalData.countryBack,
                city: app.globalData.addressBack,
            },
            success: function (res) {
                console.log(res.data);
                wx.setNavigationBarTitle({
                    title: `秘书团·${res.data.secretaryList.length}`,
                });
                if (res.data.checkinPersonNum == 0) {
                    _that.setData({
                        secretaryInfo: false,
                    });
                } else {
                    _that.setData({
                        secretaryPersonNum: res.data.secretaryList.length,
                        secretaryArr: res.data.secretaryList,
                        secretaryInfo: true,
                    });
                }
            },
        });
    },
    //打电话
    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone,
        });
    },
});
